package team.jlm.coderefactor.plugin.action

import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFileManager
import team.jlm.utils.graph.Graph
import team.jlm.utils.graph.Tarjan
import guru.nidi.graphviz.engine.Format
import guru.nidi.graphviz.engine.Graphviz
import team.jlm.coderefactor.code.IG
import team.jlm.coderefactor.plugin.ui.ImagePanel
import team.jlm.utils.file.pluginCacheFolderName
import team.jlm.utils.psi.getAllClassesInProject
import java.io.File

import mu.KotlinLogging
private val logger = KotlinLogging.logger{}

class ShowClassesAction : AnAction() {
    companion object {
        init {

        }
    }

    override fun actionPerformed(e: AnActionEvent) {
        val project = e.project

        showClassesInProject(project)
    }

}

fun showClassesInProject(project: Project?, name: String = "all_classes.png") {
    val classes = project?.let { getAllClassesInProject(it) }
//    if (classes != null) {
//        for (clazz in classes) {
//            logger.debug{ clazz.name}
//        }
//    }
    val ig = classes?.let { IG(it) }
    val t = Tarjan(ig as Graph<String>)
    val result = t.result
    for (row in result) {
        if (row.size == 1) {
            ig.delNode(row[0].data)
        } else {
            for (col in row) {
                print("${col.data} ,")
            }
            logger.debug { }
        }
    }
    val g = ig.toGraphvizGraph()
    val r = g.let { Graphviz.fromGraph(it).render(Format.PNG) }
    r.toFile(
        File(
            (project.basePath ?: System.getProperty("user.dir")) +
                    "/${pluginCacheFolderName}/${name}"
        )
    )
    val img = r.toImage()
    ImagePanel.img = img
    VirtualFileManager.getInstance().asyncRefresh { }
}